<?php
namespace Admin\Controller;
use Think\Controller;
class NewsController extends CommonController {
 public function index(){
        $conds = array();
        $title = $_GET['title'];
        if($title) {
            $conds['title'] = $title;
            $this->assign('title',$conds['title']);
        }
  $page = $_REQUEST['p'] ? $_REQUEST['p'] : 1;
  $pageSize =4;
  $news = D("News")->getNews($conds,$page,$pageSize);
  $count = D("News")->getNewsCount($conds);
  $res  =  new \Think\Page($count,$pageSize);
  $pageres = $res->show();
  $this->assign('pageres',$pageres);
  $this->assign('news', $news);
  $this->display();
 	}

  	public function add(){
    if($_POST) {
         if(!isset($_POST['title']) || !$_POST['title']) {
                return show(0,'标题不存在');
            }
            if(!isset($_POST['small_title']) || !$_POST['small_title']) {
                return show(0,'短标题不存在');
            }
            if(!isset($_POST['keywords']) || !$_POST['keywords']) {
                return show(0,'关键字不存在');
            }
            if(!isset($_POST['content']) || !$_POST['content']) {
                return show(0,'content不存在');
            }
           if($_POST['news_id']) {
                return $this->save($_POST);
            }
            $newsId= D("News")->insert($_POST);  
            if($newsId){
              $newsContentData['content'] = $_POST['content'];
              $newsContentData['news_id'] = $newsId;
              $cId = D("NewsContent")->insert($newsContentData);

              if($cId){
                 return  show(1,"新增成功");
              }else{
                 return  show(1,"主表插入成功，副表插入失败");
              }
              }else{
                 return  show(0,"新增失败");
              }
        }else {

          $this->display();
        }
    }

    public function edit(){
       $newsId =$_GET['id'];
      if(!$newsId) {
            // 执行跳转
            $this->redirect('/admin.php?c=news');
      }
    $news = D("News")->find($newsId);
        if(!$news) {
            $this->redirect('/admin.php?c=news');
        }
        $newsContent = D("NewsContent")->find($newsId);
        if($newsContent) {
            $news['content'] = $newsContent['content'];
        }
       $this->assign('news',$news);
        $this->display();


    }

 public function setStatus() {
        try {
            if ($_POST) {
                $id = $_POST['id'];
                $status = $_POST['status'];
                if (!$id) {
                    return show(0, 'ID不存在');
                }
                $res = D("News")->updateStatusById($id, $status);
                if ($res) {
                    return show(1, '操作成功');
                } else {
                    return show(0, '操作失败');
                }
            }
            return show(0, '没有提交的内容');
        }catch(Exception $e) {
            return show(0, $e->getMessage());
        }
    }
public function save($data) {
        $newsId = $data['news_id'];
        unset($data['news_id']);

        try {
            $id = D("News")->updateById($newsId, $data);
            $newsContentData['content'] = $data['content'];
            $condId = D("NewsContent")->updateNewsById($newsId, $newsContentData);
            if($id === false || $condId === false) {
                return show(0, '更新失败');
            }
            return show(1, '更新成功');
        }catch(Exception $e) {
            return show(0, $e->getMessage());
        }

    }


}